package com.hhg.test.testscroll;


import com.hhg.test.testscroll.R;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Scroller;

public class FlatWorkSpace extends ViewGroup {

    private float mTouchX;

    private float mLastMotionX;

    private int mActivePointerId = -1;

    private Scroller mScroller;


    public FlatWorkSpace(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public FlatWorkSpace(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public void initScreen() {
        Context context = getContext();
        mScroller = new Scroller(context);
        for (int i = 0; i < 3; i++) {
            this.addView(new ImageView(this.getContext()), i);
        }
        ((ImageView)this.getChildAt(0)).setImageResource(R.drawable.view_1);
        ((ImageView)this.getChildAt(1)).setImageResource(R.drawable.view_2);
        ((ImageView)this.getChildAt(2)).setImageResource(R.drawable.view_3);

    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        final long drawingTime = getDrawingTime();
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            drawChild(canvas, getChildAt(i), drawingTime);
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
        }

        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
        }

        // The children are given the same width and height as the workspace
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childLeft = 0;
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                final int childWidth = child.getMeasuredWidth();
                child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight());
                childLeft += childWidth;
            }
        }

    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            mTouchX = mScroller.getCurrX();
            int scrollY = mScroller.getCurrY();
            scrollTo((int)mTouchX, scrollY);
            postInvalidate();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            mLastMotionX = ev.getX();
            mActivePointerId = ev.getPointerId(0);
            break;
        case MotionEvent.ACTION_MOVE:

            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            try {
                final float x = ev.getX(pointerIndex);
                final float deltaX = mLastMotionX - x;
                mLastMotionX = x;
                if (deltaX > 0) {
                        scrollBy((int)deltaX, 0);
                } else if (deltaX < 0) {
                        scrollBy((int)deltaX, 0);
                } else {
                    awakenScrollBars();
                }
            } catch (ArrayIndexOutOfBoundsException e) {

            }
            break;
        case MotionEvent.ACTION_UP:
            int screenWidth = getWidth();
            int whichView = (getScrollX() + (screenWidth / 2)) / screenWidth;
            whichView = Math.max(0, Math.min(whichView, getChildCount() - 1));
            int newX = whichView * getWidth();
            int delta = newX - getScrollX();
            mScroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2);
            invalidate();
            MainActivity.setLevel(whichView);
            break;
        case MotionEvent.ACTION_CANCEL:
            break;
        case MotionEvent.ACTION_POINTER_UP:
            break;
        }

        return true;
    }

}

